﻿using Activity.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Activity.Helper
{
    /// <summary>
    /// 微信API基类
    /// </summary>
    // ReSharper disable once InconsistentNaming
    public abstract class BaseAPICommand
    {
        public abstract HttpMethod RequestType { get; }
        public abstract string Url { get; }
        public ErrorMessage Result
        {
            get;
            protected set;
        }
        public virtual T Request<T>(string Data) where T : ErrorMessage, new()
        {
            T r;
            bool loop=false;
            const int c = 10;
            var i = 0;
            do
            {
                r = this.RequestType == HttpMethod.GET
                    ? HttpClientHelper.GetResponse<T>(this.Url)
                    : HttpClientHelper.PostResponse<T>(this.Url, Data);
                Log.WriteLog("Request \n{0}\n{1}", "", Log.LogType.INFO, this.Url, Data);
                if (r == null) { loop = true; }
                else
                {
                    this.Result = r;
                    //loop = AccessToken.CheckAndRefresh(r.ErrCode);
                }
                i++;
            } while (loop && i < c);
            return r;
        }
    }

    public enum HttpMethod
    {
        GET,
        POST
    }
}
